package com.company.cloud.coupon.modules.mgr.service.impl;

import com.company.cloud.coupon.constant.CouponConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.constant.BaseConstant.YNEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.common.utils.LocalDateTimeUtils;
import com.company.cloud.common.utils.StrSplitUtils;
import com.company.cloud.coupon.constant.CouponConstant.CouponConfigTypeEnum;
import com.company.cloud.coupon.constant.CouponConstant.CouponUsableObjTypeEnum;
import com.company.cloud.coupon.constant.CouponErrorEnum;
import com.company.cloud.coupon.modules.base.entity.CouponConfigInfo;
import com.company.cloud.coupon.modules.base.service.CouponConfigInfoService;
import com.company.cloud.coupon.modules.mgr.controller.req.CouponConfigSaveParam;
import com.company.cloud.coupon.modules.mgr.controller.req.CouponConfigUpdateParam;
import com.company.cloud.coupon.modules.mgr.service.CouponConfigMgrService;
import org.springframework.util.StringUtils;

@Service
public class CouponConfigMgrServiceImpl implements CouponConfigMgrService{

	
	@Autowired
	private CouponConfigInfoService couponConfigInfoService;
	
	
	@Transactional
	@Override
	public void save(CouponConfigSaveParam params, LoginSimpleUserInfo userInfo) {
		ValidatorUtils.validate(params);
		CouponConfigInfo couponConfig=new CouponConfigInfo();
		
		couponConfig.setCompanyNo(userInfo.getCompanyNo());
		couponConfig.setTitle(params.getTitle());
		couponConfig.setIsUseLimit(params.getIsUseLimit());
		
		if(YNEnum.YES.getCode().equals(params.getIsUseLimit())) {
			if(!CheckUtils.hasBigDecimal(params.getUseFullFund())) {
				throw new BusinessException(CouponErrorEnum.Coupon_FullFund);
			}
			couponConfig.setUseFullFund(params.getUseFullFund());
		}
		
		couponConfig.setCouponType(params.getCouponType());
		couponConfig.setCouponFund(params.getCouponFund());
		if(CouponConfigTypeEnum.Random_Cash_Coupon.getCode().equals(params.getCouponType())) {
			if(!CheckUtils.hasBigDecimal(params.getCouponFundMax())) {
				throw new BusinessException(CouponErrorEnum.Coupon_FullFund);
			}
			couponConfig.setCouponFundMax(params.getCouponFundMax());
		}
		
		couponConfig.setIsMaxDisFund(params.getIsMaxDisFund());
		couponConfig.setMaxDisFund(params.getMaxDisFund());
	
		
		couponConfig.setUseDateType(params.getUseDateType());
		if(CouponConstant.CouponEndDateTypeEnum.Appointed_EndDate.getCode().equals(params.getUseDateType())){
			if(StringUtils.isEmpty(params.getUseStartDate())){
				throw new BusinessException(CouponErrorEnum.Coupon_UseDateNull);
			}
			if(StringUtils.isEmpty(params.getUseEndDate())){
				throw new BusinessException(CouponErrorEnum.Coupon_UseDateNull);
			}
			couponConfig.setUseStartDate(LocalDateTimeUtils.parse_yyyyMMddHHmmss(params.getUseStartDate()));
			couponConfig.setUseEndDate(LocalDateTimeUtils.parse_yyyyMMddHHmmss(params.getUseEndDate()));
		}else{
			if(!CheckUtils.hasInt(params.getUseEndDay())){
				throw new BusinessException(CouponErrorEnum.Coupon_UseDateNull);
			}
			couponConfig.setUseEndDay(params.getUseEndDay());
		}


		
		couponConfig.setUsableObjType(params.getUsableObjType());
		if(CouponUsableObjTypeEnum.Appointed_UsableObj.getCode().equals(params.getUsableObjType())) {
			if(CollectionUtils.isEmpty(params.getUsableObjNo())) {
				throw new BusinessException(CouponErrorEnum.Coupon_UsableObj);
			}
			couponConfig.setUsableObjNo(StrSplitUtils.list2string_prefix(params.getUsableObjNo()));
		}
		couponConfig.setTotalNumType(params.getTotalNumType());
		couponConfig.setTotalNum(params.getTotalNum());
		couponConfig.setCouponGetNum(0);
		couponConfig.setCouponUseNum(0);
		couponConfig.setMemberGetNumType(params.getMemberGetNumType());
		couponConfig.setMemberGetNum(params.getMemberGetNum());
		couponConfig.setMemberUseType(params.getMemberUseType());
		couponConfig.setMemberUse(StrSplitUtils.list2string(params.getMemberUse()));
		couponConfig.setUpperLowerState(params.getUpperLowerState());
		
		couponConfig.setIsRemind(params.getIsRemind());
		couponConfig.setRemindDay(params.getRemindDay());
		couponConfig.setShareSetting(StrSplitUtils.list2string(params.getShareSetting()));
		couponConfig.setOtherSetting(StrSplitUtils.list2string(params.getOtherSetting()));
		couponConfig.addCreateParam(userInfo);
		couponConfig.addEditParam(userInfo);
		couponConfigInfoService.save(couponConfig);
		
	}


	@Transactional
	@Override
	public void update(CouponConfigUpdateParam params, LoginSimpleUserInfo userInfo) {
		ValidatorUtils.validate(params);
		CouponConfigInfo couponConfig=couponConfigInfoService.getById(params.getId());
		if(couponConfig==null || EnabledEnum.DISABLED.getCode().equals(couponConfig.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		if(YNEnum.YES.getCode().equals(couponConfig.getUpperLowerState())) {
			throw new BusinessException(CouponErrorEnum.Coupon_UpperLower);
		}
		
		couponConfig.setTitle(params.getTitle());
		couponConfig.setIsUseLimit(params.getIsUseLimit());
		if(YNEnum.YES.getCode().equals(params.getIsUseLimit())) {
			if(!CheckUtils.hasBigDecimal(params.getUseFullFund())) {
				throw new BusinessException(CouponErrorEnum.Coupon_FullFund);
			}
			couponConfig.setUseFullFund(params.getUseFullFund());
		}
		couponConfig.setCouponType(params.getCouponType());
		couponConfig.setCouponFund(params.getCouponFund());
		if(CouponConfigTypeEnum.Random_Cash_Coupon.getCode().equals(params.getCouponType())) {
			if(!CheckUtils.hasBigDecimal(params.getCouponFundMax())) {
				throw new BusinessException(CouponErrorEnum.Coupon_FullFund);
			}
			couponConfig.setCouponFundMax(params.getCouponFundMax());
		}
		couponConfig.setIsMaxDisFund(params.getIsMaxDisFund());
		couponConfig.setMaxDisFund(params.getMaxDisFund());
		
		couponConfig.setUseDateType(params.getUseDateType());
		couponConfig.setUseStartDate(LocalDateTimeUtils.parse_yyyyMMddHHmmss(params.getUseStartDate()));
		couponConfig.setUseEndDate(LocalDateTimeUtils.parse_yyyyMMddHHmmss(params.getUseEndDate()));
		couponConfig.setUseEndDay(params.getUseEndDay());
		
		couponConfig.setUsableObjType(params.getUsableObjType());
		if(CouponUsableObjTypeEnum.Appointed_UsableObj.getCode().equals(params.getUsableObjType())) {
			if(CollectionUtils.isEmpty(params.getUsableObjNo())) {
				throw new BusinessException(CouponErrorEnum.Coupon_UsableObj);
			}
			couponConfig.setUsableObjNo(StrSplitUtils.list2string_prefix(params.getUsableObjNo()));
		}
		couponConfig.setTotalNumType(params.getTotalNumType());
		couponConfig.setTotalNum(params.getTotalNum());
		couponConfig.setMemberGetNumType(params.getMemberGetNumType());
		couponConfig.setMemberGetNum(params.getMemberGetNum());
		couponConfig.setMemberUseType(params.getMemberUseType());
		couponConfig.setMemberUse(StrSplitUtils.list2string(params.getMemberUse()));
		couponConfig.setUpperLowerState(params.getUpperLowerState());
		
		couponConfig.setIsRemind(params.getIsRemind());
		couponConfig.setRemindDay(params.getRemindDay());
		couponConfig.setShareSetting(StrSplitUtils.list2string(params.getShareSetting()));
		couponConfig.setOtherSetting(StrSplitUtils.list2string(params.getOtherSetting()));
		couponConfig.addEditParam(userInfo);
		couponConfigInfoService.updateById(couponConfig);
		
	}
	
	
	
	
}
